# Maintainer: Antoine Lambert <antoine.lambert@inria.fr>

_realname=tulip
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.0.1
pkgrel=1
pkgdesc="Large graphs analysis, drawing and visualization framework (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://tulip.labri.fr/"
license=('spdx:LGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-libzip"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
         "${MINGW_PACKAGE_PREFIX}-yajl"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-sip"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://sourceforge.net/projects/auber/files/tulip/${_realname}-${pkgver}/${_realname}-${pkgver}_src.tar.gz"
        "001-fix-tulip-python-install.patch"
        "002-fix-template-instatiation.patch"
        "003-fix-clang-omp-link.patch"
        "004-fix-tulip-core-headers.patch"
        "005-fix-cmakelists.patch"
        "009-clang-sframe.patch"
        "010-aarch64.patch")
noextract=("${_realname}-${pkgver}_src.tar.gz")
sha256sums=('d67cfbaa8cea78272bbc52b5f09a49ffd8eef085c6f659ba9c9b9cd53e7661dd'
            '95410b6c9eeaf306e250ebae56c3d46d435014e4bfa723dd464cf47f5f061b05'
            '8572c14d233a9026fcc984ab9d94e1c8230eadee63ff2eaeed6ad01e5063610b'
            '20bc5ed1744505ab89a4aec597e5286451d37c49241b8a784e0a51957f43dc7b'
            'ace5318108e15b61202440e112ed2ac81d49dd44fa5d6cc3766f09da8299b6b5'
            '7f5f822b44332cbf1aa609bc970d2db298bfe779b35ad3e28e64344e0b709baa'
            '3b4c997251974553111aaf5e55b041a319aeabd048e9383aca3b8778bb11b1f6'
            '248298cf888c2f81bf8fdc690d67ff171115a904a30ae9f0e5524c8a286ed1a2')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}"/${_patch}
  done
}

prepare() {
  tar -xf "${_realname}-${pkgver}_src.tar.gz" || tar -xf "${_realname}-${pkgver}_src.tar.gz"

  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    001-fix-tulip-python-install.patch \
    002-fix-template-instatiation.patch \
    003-fix-clang-omp-link.patch \
    004-fix-tulip-core-headers.patch \
    005-fix-cmakelists.patch

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    apply_patch_with_msg \
      009-clang-sframe.patch
  fi 

  apply_patch_with_msg \
    010-aarch64.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-dtor-name"
    _extra_config+=("-DCLANG=ON")
  else
    CXXFLAGS+=" -Wno-template-id-cdtor"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DCMAKE_NEED_RESPONSE=ON \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DTULIP_FIXUP_BUNDLE=OFF \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python3.exe \
    -DTULIP_USE_QT6=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING.LESSER "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER
}
